﻿using Entity.Package;
using System;
using System.Data;
using System.IO;
using System.Text;

namespace BLL.Business
{
    public class FileUtil
    {
        /// <summary>
        /// 读取Txt文件
        /// </summary>
        /// <param name="filePath">文件路径 eg：D:\A.csv</param>
        public static DataTable ReadTxt(string filePath)
        {
            var dt = new DataTable();
            if (File.Exists(filePath))
            {
                FileStream fs = null; StreamReader sr = null;
                try
                {
                    fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    sr = new StreamReader(fs, Encoding.UTF8);
                    //记录每次读取的一行记录
                    string strLine = null;
                    //记录每行记录中的各字段内容
                    string[] arrayLine = null;
                    //分隔符
                    string[] separators = { "," };
                    //添加列名
                    dt.Columns.Add("GasUserID", typeof(string));//用户编号
                    dt.Columns.Add("PayMoney", typeof(decimal));//收费金额
                    dt.Columns.Add("FlowNumber", typeof(string));//流水号
                    dt.Columns.Add("PayDate", typeof(string));//收费日期

                    //逐行读取txt文件
                    while ((strLine = sr.ReadLine()) != null)
                    {
                        //去除头尾空格
                        strLine = strLine.Trim();
                        //分隔字符串，返回数组
                        arrayLine = strLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                        //添加行
                        DataRow dataRow = dt.NewRow();
                        for (int j = 0; j < arrayLine.Length; j++)
                        {
                            dataRow[j] = arrayLine[j].Replace(",", "");
                        }
                        dt.Rows.Add(dataRow);
                    }
                }
                catch
                {
                    dt = null;
                }
                finally
                {
                    sr.Close();
                    fs.Close();
                }
            }
            else
            {
                dt = null;
            }
            return dt;
        }

        /// <summary>
        /// 生成对账文件
        /// </summary>
        /// <param name="fileData"></param>
        /// <returns></returns>
        public static bool WriteTraceFile(PackTraceFileData fileData, out string errMsg)
        {
            errMsg = "";
            StreamWriter streamWriter = null;
            try
            {
                if (!Directory.Exists(fileData.FilePath))
                    Directory.CreateDirectory(fileData.FilePath);

                streamWriter = new StreamWriter(fileData.FilePath + "\\" + fileData.FileName, false);
                //总数
                streamWriter.WriteLine(fileData.Total);
                //流水
                foreach (var item in fileData.TraceRows)
                {
                    streamWriter.WriteLine(item);
                }
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return false;
            }
            finally
            {
                if (streamWriter != null)
                {
                    streamWriter.Close();
                    streamWriter.Dispose();
                }
            }
            return true;
        }
    }
}
